#SingleInstance force
U_GVIM := "C:\Program Files\Vim\vim72\gvim.exe"
U_LOG := "c:\working\build.log"

IfExist, navisworks.ico
   Menu, tray, icon, navisworks.ico

SetTimer, timer, 10000
return

timer:
FormatTime, time, , HHmm
if ( time = 0400 )
{
   ;fun_build_roamer()
   sleep 1000 * 60
}
return

^!b::
;fun_build_roamer("tang", "r32", false)
fun_build_roamer("Willin", "32", false)
return

^!t:: run, %U_GVIM% %U_LOG%

^!a:: ;edit the AutoHotKey Script
IfWinExist %A_ScriptName%
{
   WinClose
   Reload
}
else
{
   run, %U_GVIM% %A_ScriptFullPath%
   WinWait, %A_ScriptName%
   WinActivate
   ;WinMaximize
}
return

fun_build_roamer(p4_client = "tang", target = "32", if_record_change = true) ; tang/Willin | 32/d32/r32 | true/false
{
   U_LOG := "c:\working\build.log"

   fun_copy_assembly()
   fun_login_p4()

   ; get the status of p4 ticket
   RunWait, %comspec% /c p4 login -s > c:\MyTempFile.txt, , Hide
   FileReadLine, str, C:\MyTempFile.txt, 1
   FileDelete, C:\MyTempFile.txt
   StringSplit, word_array, str, %A_Space%

   if ("tangw" = word_array2 and 1 < word_array6)
   {
      ; get the change numbler
      RunWait, %comspec% /c p4 changes -s submitted -m 1 > c:\MyTempFile.txt, , Hide
      FileReadLine, CHANGE_LIST, C:\MyTempFile.txt, 1
      FileDelete, C:\MyTempFile.txt
      StringSplit, change_array, CHANGE_LIST, %A_Space%
      ; get the time
      FormatTime, LOG_TIME, , yyyy_MM_dd_HH_mm

      IniRead, last_change, ahk_setting.ini, roamer, last_change, 0
      if ( !if_record_change || change_array2 <> last_change )
      {
         if (p4_client == "tang")
         {
            project_name := "tang"
         }
         if (p4_client == "Willin")
         {
            project_name := "NavisWorks"
         }
         EnvSet, P4CLIENT, %p4_client%
         log_path = c:\log\%change_array2%_%LOG_TIME%.log

         FormatTime, LOG_TIME, , yyyy-MM-dd HH:mm
         RunWait, %comspec% /c echo. >> %U_LOG%
         RunWait, %comspec% /c echo %change_array2% >> %U_LOG%
         RunWait, %comspec% /c echo %LOG_TIME%>> %U_LOG%
         fun_build(target, project_name, log_path)
         FormatTime, LOG_TIME, , yyyy-MM-dd HH:mm
         RunWait, %comspec% /c echo %LOG_TIME%>> %U_LOG%
       
         p := "Win32" ; x64"

         if ( target = "32" )
            v := "dddt dodt"
         else if ( target = "r32" )
            v := "dodt"
         else if ( target = "d32" )
            v := "dddt"
         else
            v := "dddt dodt"

         i = 0
         ii = 0
         Loop, Parse, p, %A_Space%
         {
            pp = %A_LoopField%
            Loop, Parse, v, %A_Space%
            {
               vv = %A_LoopField%
               ii++
               IfExist, c:\%project_name%\main\projects\vc9\%pp%\%vv%\Roamer.exe
               {
                  i++
               }
            }
         }
         if ( i = ii )
         {
            if (if_record_change)
            {
               IniWrite, %change_array2%, ahk_setting.ini, roamer, last_change
            }
            run, c:\working\isgtool email -f Navisworks.ACRD.DailyBuilder@autodesk.com -t Wei.Tang@autodesk.com -s "Success-%change_array2%-Navisworks daily build event" -m Success
         }
         else
         {
            run, c:\working\isgtool email -f Navisworks.ACRD.DailyBuilder@autodesk.com -t Wei.Tang@autodesk.com -s "Failure-%change_array2%-Navisworks daily build event" -m Failure
         }
      }
      else
      {
         FormatTime, LOG_TIME, , yyyy-MM-dd HH:mm
         RunWait, %comspec% /c echo. >> %U_LOG%
         RunWait, %comspec% /c echo %LOG_TIME%>> %U_LOG%
         RunWait, %comspec% /c echo There is no new change. >> %U_LOG%
         run, c:\working\isgtool email -f Navisworks.ACRD.DailyBuilder@autodesk.com -t Wei.Tang@autodesk.com -s "NoChange-%change_array2%-Navisworks daily build event" -m NoChange
      }
   }
   else
   {
      FormatTime, LOG_TIME, , yyyy-MM-dd HH:mm
      RunWait, %comspec% /c echo. >> %U_LOG%
      RunWait, %comspec% /c echo %LOG_TIME%>> %U_LOG%
      RunWait, %comspec% /c echo The p4 is invalid. >> %U_LOG%
      run, c:\working\isgtool email -f Navisworks.ACRD.DailyBuilder@autodesk.com -t Wei.Tang@autodesk.com -s "P4Invalid-Navisworks daily build event" -m P4Invalid
   }
}

fun_build(target, project_name, log_path)
{
   SetWorkingDir, c:\%project_name%\main\projects

   ; 1. sync p4
   FormatTime, LOG_TIME, , HH:mm
   RunWait, %comspec% /c echo sync p4 at %LOG_TIME% >> %log_path%
   RunWait, %comspec% /c echo. >> %log_path%
   RunWait, %comspec% /c p4 sync c:\%project_name%\main\...#head
   RunWait, %comspec% /c echo. >> %log_path%

   ; 2. clean build
   FormatTime, LOG_TIME, , HH:mm
   RunWait, %comspec% /c echo clean build at %LOG_TIME% >> %log_path%
   RunWait, %comspec% /c echo. >> %log_path%
   RunWait, %comspec% /c n %target% clean
   RunWait, %comspec% /c echo. >> %log_path%

   ; 3. copy assemblies
   FormatTime, LOG_TIME, , HH:mm
   RunWait, %comspec% /c echo copy assemblies at %LOG_TIME% >> %log_path%
   RunWait, %comspec% /c echo. >> %log_path%
   
   p := "Win32" ; x64"

   if ( target = "32" )
      v := "dddt dodt"
   else if ( target = "r32" )
      v := "dodt"
   else if ( target = "d32" )
      v := "dddt"
   else
      v := "dddt dodt"

   f := "navisworks.gui.common.dll navisworks.gui.common.pdb navisworks.gui.generic.dll navisworks.gui.generic.pdb"

   Loop, Parse, v, %A_Space%
   {
      IfNotExist, c:\%project_name%\main\projects\vc9\Win32\%A_LoopField%
         FileCreateDir, c:\%project_name%\main\projects\vc9\Win32\%A_LoopField%
   }
      
   Loop, Parse, p, %A_Space%
   {
      pp = %A_LoopField%
      Loop, Parse, v, %A_Space%
      {
         vv = %A_LoopField%
         Loop, Parse, f, %A_Space%
         {
            ff = %A_LoopField%
            FileCopy, c:\working\assemblies\vc9\%pp%\%vv%\%ff%, c:\%project_name%\main\projects\vc9\%pp%\%vv%\%ff%, 1
         }
      }
   }
   RunWait, %comspec% /c echo. >> %log_path%

   ; 4. build roamer
   FormatTime, LOG_TIME, , HH:mm
   RunWait, %comspec% /c echo build roamer at %LOG_TIME% >> %log_path%
   RunWait, %comspec% /c echo. >> %log_path%

   ;EnvSet, NW_NET_API_VERSION, 999
   RunWait, %comspec% /c n %target% build_roamer
   RunWait, %comspec% /c echo. >> %log_path%

   SetWorkingDir, %A_ScriptDir%
}

fun_login_p4()
{  
   RunWait, %comspec% /c p4 logout
   run, %comspec% /c p4 login
   WinWaitActive, C:\WINDOWS\system32\cmd.exe
   SendPlay, tw820826{enter}
   sleep, 3000
}

fun_copy_assembly()
{
   p := "Win32" ; x64"
   v := "dddt dodt"
   f := "navisworks.gui.common.dll navisworks.gui.common.pdb navisworks.gui.generic.dll navisworks.gui.generic.pdb"
   change_dir = ""
   latest_change_dir = ""
   Loop, \\shffs1\Builds\main\*, 2
   {
      change_dir = %A_LoopFileName%
      i = 0
      ii = 0
      Loop, Parse, p, %A_Space%
      {
         pp = %A_LoopField%
         Loop, Parse, v, %A_Space%
         {
            vv = %A_LoopField%
            Loop, Parse, f, %A_Space%
            {
               ff = %A_LoopField%
               ii++
               IfExist, \\shffs1\Builds\main\%change_dir%\assemblies\vc9\%pp%\%vv%\%ff%
               {
                  i++
               }
            }
         }
      }
      if (i=ii)
      {
         latest_change_dir := change_dir
      }
   }
   if (latest_change_dir != "")
   {
      StringLeft, change_num, latest_change_dir, 5
      IniRead, last_ass_change, ahk_setting.ini, roamer, last_ass_change, 0
      if ( change_num > last_ass_change)
      {
         Loop, Parse, p, %A_Space%
         {
            pp = %A_LoopField%
            Loop, Parse, v, %A_Space%
            {
               vv = %A_LoopField%
               Loop, Parse, f, %A_Space%
               {
                  ff = %A_LoopField%
                  FileCopy, \\shffs1\Builds\main\%latest_change_dir%\assemblies\vc9\%pp%\%vv%\%ff%, c:\working\assemblies\vc9\%pp%\%vv%\%ff%, 1
               }
            }
         }
         IniWrite, %change_num%, ahk_setting.ini, roamer, last_ass_change
      }
   }
}

